// 获取当月多少天
function getMonthOfDay () {
  const date = new Date()

  const year = date.getFullYear()
  const month = date.getMonth() + 1

  const monthOfDay = new Date(year, month, 0).getDate()

  return monthOfDay
}

// console.log(getMonthOfDay())

// ===========================================================================

// 斐波数列（获取第n项）
function getFiber (n) {
  if (n === 1 || n === 2) return 1

  return getFiber(n - 1) + getFiber(n - 2)
}

const result = getFiber(12)

// console.log('result', result)

// ===========================================================================

// 数组分割
// 方法一：
function chunk (arr, size = 1) {
  const bigArr = []
  let smallArr = []

  if (arr.length === 0) return bigArr

  if (size < 0) {
    size = 1
  }

  arr.forEach(item => {
    if (smallArr.length === 0) {
      bigArr.push(smallArr)
    }

    smallArr.push(item)

    if (smallArr.length === size) {
      smallArr = []
    }
  })

  return bigArr
}

// 方法二：
function chunk1 (arr, size = 1) {
  const result = []

  for (let i = 0; i < arr.length; i+=size) {
    result.push(arr.slice(i, i + size))
    // arr.slice(0, 2) [1, 4]
    // arr.slice(2, 4) [6, 2]
    // arr.slice(4, 6) [4, 4]
  }

  return result
}

const arr = [1, 4, 6, 2, 4, 4]

console.log(chunk(arr, 3))

// console.log(chunk1(arr, 3))

// ===========================================================================

const add = (a, b) => a + b
const minus = (a, b) => a - b
const multiply = (a, b) => a * b
const divide = (a, b) => a / b

const calc = (a, b, callback) => callback(a, b)

const value1 = calc(3, 4, add)
const value2 = calc(3, 4, minus)
const value3 = calc(3, 4, multiply)
const value4 = calc(3, 4, divide)

// console.log('add', value1)
// console.log('minus', value2)
// console.log('multiply', value3)
// console.log('divide', value4)

// ===========================================================================

// class类
class Person {
  constructor (name, age) {
    this.name = name
    this.age = age
  }

  say () {
    console.log('my name is ' + this.name)
  }
}

const p = new Person('张三', 18)

// p.say()

class Student extends Person {
  constructor (name, age, sex) {
    super(name, age)
    this.sex = sex
  }

  say () {
    super.say()
    console.log('我的名字叫' + this.name)
  }

  cry () {
    console.log('我今年' + this.age + '岁了')
  }
}

const s = new Student('李四', 3, 'sex')

s.say()
s.cry()
